<template>
  <div class="config-gateway">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="0">
            <a-col :md="6" :sm="24">
              <a-form-item label="网关名称">
                <a-input
                  v-model="queryParam.gatewayName"
                  allow-clear
                  placeholder="请输入网关名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="10" :sm="24">
              <span
                class="table-page-search-submitButtons"
                style="float: left; overflow: hidden;padding-left: 16px;">
                <a-button
                  type="primary"
                  class="btn_search_reset"
                  @click="$refs.table.refresh(true)">
                  <a-icon
                    type="search"/>
                  查询
                </a-button>
              </span>
            </a-col>
            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                style="float: right; overflow: hidden;">
                <a-button
                  class="btn_add"
                  type="primary"
                  @click="handleBatchDataReportStatus(1)">一键开启数据上报</a-button>
                <a-button
                  class="btn_add"
                  type="primary"
                  @click="handleBatchDataReportStatus(0)">一键关闭数据上报</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>

        <s-table
          id="table-wrapper"
          ref="table"
          size="middle"
          :columns="columns"
          :data="loadData"
          :alert="true"
          :rowKey="(record) => record.id"
        >

          <span slot="status" slot-scope="text">
            {{ getStatus(text) }}
          </span>

          <span slot="gatewayName" slot-scope="text, record">
            <template v-if="text !== null">
              {{ record.gatewayName }}
            </template>
            <template v-if="text === null">
              未命名设备-{{ record.gatewayCode }}
            </template>
          </span>

          <span slot="onlineStatus" slot-scope="text">
            {{ getOnlineStatus(text) }}
          </span>

          <span slot="dataReportingSwitch" slot-scope="text, record">
            <a-popconfirm placement="top" :title="text===0? '确定打开？':'确定关闭？'" @confirm="() => changeSwitch(record)">
              <a>{{ text === 1? '开':'关' }}</a>
            </a-popconfirm>
          </span>

          <span slot="action" slot-scope="text, record">
            <a @click="$refs.editForm.edit(record,stationId)" class="btn_edit">编辑</a>
          </span>
        </s-table>
      </div>

      <add-form ref="addForm" @ok="handleOk"/>
      <edit-form ref="editForm" @ok="handleOk"/>
      <import-form ref="importForm" @ok="handleOk"/>
      <export-form ref="exportForm" @ok="handleOk"/>
    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import {
    guidePageGataway,
    del,
    selfTest,
    changeSwitch,
    batchChangeDataReportStatus
  } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { Empty } from 'ant-design-vue'
  import addForm from './gateway/addForm'
  import editForm from './gateway/editForm'
  import importForm from '../bizgateway/importForm'
  import exportForm from '../bizgateway/exportForm'

  export default {
    components: {
      STable,
      addForm,
      importForm,
      editForm,
      exportForm
    },

    props: {
      stationId: {
        type: Number,
        require: false
      }
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '网关名称',
            dataIndex: 'gatewayName',
            scopedSlots: { customRender: 'gatewayName' }
          },
          {
            title: '网关状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
            align: 'center'
          },
          {
            title: '运行状态',
            dataIndex: 'onlineStatus',
            scopedSlots: { customRender: 'onlineStatus' },
            align: 'center'
          },
          {
            title: '设备IP',
            dataIndex: 'gatewayIp'
          },
          {
            title: '数据上报开关',
            dataIndex: 'dataReportingSwitch',
            scopedSlots: { customRender: 'dataReportingSwitch' },
            align: 'center'
          },
          {
            title: '创建日期',
            dataIndex: 'createdAt',
            width: 150,
            align: 'center'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' },
            width: 250,
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return guidePageGataway(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        gatewayLoading: true,
        simpleImage: Empty.PRESENTED_IMAGE_SIMPLE,
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        btnLoading: false
      }
    },

    created () {
      this.queryParam.stationId = this.stationId
    },

    methods: {
      del (record) {
        del({
          id: record.id
        }).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      getStatus (text) {
        if (text === 1) {
          return '开启'
        } else {
          return '禁用'
        }
      },
      getOnlineStatus (text) {
        if (text === 1) {
          return '在线'
        } else {
          return '离线'
        }
      },
      changeSwitch (record) {
        if (record.dataReportingSwitch === 1) {
          record.dataReportingSwitch = 0
        } else {
          record.dataReportingSwitch = 1
        }
        changeSwitch({ id: record.id, dataReportingSwitch: record.dataReportingSwitch }).then(res => {
          if (res.success) {
            this.$message.success('操作成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('操作失败：' + res.message)
          }
        })
      },
      // 一键开启或关闭数据上报开关
      handleBatchDataReportStatus (status) {
        const content = status === 1 ? '确定一键开启站点下所有网关的数据上报？' : '确定一键关闭站点下所有网关的数据上报？'
        const param = {
          dataReportingSwitch: status,
          stationId: this.stationId
        }
        this.$confirm({
          title: '提示',
          content: content,
          okText: '确定',
          cancelText: '取消',
          onOk: () => {
            batchChangeDataReportStatus(param).then(res => {
              if (res.success) {
                this.$message.success('操作成功')
                this.$refs.table.refresh()
              } else {
                this.$message.error('操作失败：' + res.message)
              }
            })
          },
          onCancel () {
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleClick (e) {
        this.stationId = e.toString()
        this.queryParam.stationId = e.toString()
        this.handleOk()
      },
      selfTest (record) {
        this.btnLoading = true
        selfTest({ ip: record.gatewayIp }).then(res => {
          this.btnLoading = false
          if (res.success) {
            this.$message.success(res.data)
          } else {
            this.$message.error(res.message)
          }
        })
      },
      isShow () {
        if (this.stationId) {
          return true
        }
        return false
      }
    }
  }
</script>

<style scoped lang="less">
  /*@import '../~@/assets/css/list.less';*/
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }

  .ant-tree li {
    overflow: no-content !important;
    white-space: pre-wrap !important;
  }
</style>
